#ifndef CUSLISTMODEL_H
#define CUSLISTMODEL_H

#include <QObject>
#include <QAbstractListModel>
#include <QMap>
#include <QString>

class ListModelItem: public QObject
{
    Q_OBJECT
public:
    enum DataType{
        E_PrinterID = 0,
        E_PrinterState,
        E_HasCamera,
        E_DeviceName,
        E_IpAddr,
        E_GCodeName = 5,
        E_TotalPrintTime,
        E_LeftTime,
        E_CurPrintLayer,
        E_PrintSpeed,
        E_BedDstTemp = 10,
        E_NozzleDstTemp,
        E_BedTemp,
        E_NozzleTemp,
        E_GroupNo,
        E_X = 15,
        E_Y,
        E_Z,
        E_FanOpened,
        E_LedOpened
    };

    Q_PROPERTY(QString pcPrinterID READ pcPrinterID WRITE setPcPrinterID NOTIFY pcPrinterIDChanged)
    Q_PROPERTY(int pcPrinterState READ pcPrinterState WRITE setPcPrinterState NOTIFY pcPrinterStateChanged)
    Q_PROPERTY(bool pcHasCamera READ pcHasCamera NOTIFY pcHasCameraChanged)
    Q_PROPERTY(QString pcDeviceName READ pcDeviceName WRITE setPcDeviceName NOTIFY pcDeviceNameChanged)
    Q_PROPERTY(QString pcIpAddr READ pcIpAddr NOTIFY pcIpAddrChanged)
    Q_PROPERTY(QString pcGCodeName READ pcGCodeName NOTIFY pcGCodeNameChanged)
    Q_PROPERTY(QString pcTotalPrintTime READ pcTotalPrintTime NOTIFY pcTotalPrintTimeChanged)
    Q_PROPERTY(QString pcLeftTime READ pcLeftTime NOTIFY pcLeftTimeChanged)
    Q_PROPERTY(int pcCurPrintLayer READ pcCurPrintLayer NOTIFY pcCurPrintLayerChanged)
    Q_PROPERTY(int pcPrintSpeed READ pcPrintSpeed WRITE setPcPrintSeed NOTIFY pcPrintSpeedChanged)
    Q_PROPERTY(int pcBedDstTemp READ pcBedDstTemp WRITE setPcBedDstTemp NOTIFY pcBedDstTempChanged)
    Q_PROPERTY(int pcNozzleDstTemp READ pcNozzleDstTemp WRITE setPcNozzleDstTemp NOTIFY pcNozzleDstTempChanged)
    Q_PROPERTY(int pcBedTemp READ pcBedTemp NOTIFY pcBedTempChanged)
    Q_PROPERTY(int pcNozzleTemp READ pcNozzleTemp NOTIFY pcNozzleTempChanged)
    Q_PROPERTY(QString pcGroupNo READ pcGroupNo WRITE setPcGroupNo NOTIFY pcGroupNoChanged)
    Q_PROPERTY(int pcX READ pcX WRITE setPcX NOTIFY pcXChanged)
    Q_PROPERTY(int pcY READ pcY WRITE setPcY NOTIFY pcYChanged)
    Q_PROPERTY(int pcZ READ pcZ WRITE setPcZ NOTIFY pcZChanged)
    Q_PROPERTY(bool fanOpened READ fanOpened WRITE setFanOpened NOTIFY fanOpenedChanged)
    Q_PROPERTY(bool ledOpened READ ledOpened WRITE setLedOpened NOTIFY ledOpenedChanged)

    ListModelItem(QObject* parent = nullptr);
    ListModelItem(const ListModelItem& listModelItem);
    ~ListModelItem();

    const QString &pcPrinterID() const;
    int pcPrinterState() const;
    void setPcPrinterState(int state);
    bool pcHasCamera() const;
    const QString &pcDeviceName() const;
    void setPcDeviceName(const QString& deviceName);
    const QString &pcIpAddr() const;
    const QString &pcGCodeName() const;
    const QString &pcTotalPrintTime() const;
    const QString &pcLeftTime() const;
    int pcCurPrintLayer() const;
    int pcPrintSpeed() const;
    void setPcPrintSeed(int speed);

    int pcBedDstTemp() const;
    void setPcBedDstTemp(int newPcBedDstTemp);
    int pcNozzleDstTemp() const;
    void setPcNozzleDstTemp(int newPcNozzleDstTemp);
    int pcBedTemp() const;
    int pcNozzleTemp() const;
    int pcX() const;
    void setPcX(int newPcX);
    int pcY() const;
    void setPcY(int newPcY);
    int pcZ() const;
    void setPcZ(int newPcZ);
    void setPcPrinterID(const QString &newPcPrinterID);
    bool fanOpened() const;
    void setFanOpened(bool newFanOpened);
    bool ledOpened() const;
    void setLedOpened(bool newLedOpened);
    QString pcGroupNo() const;
    void setPcGroupNo(const QString& newPcGroupNo);

    friend class CusListModel;
signals:
    void pcPrinterStateChanged();
    void pcHasCameraChanged();
    void pcDeviceNameChanged();
    void pcIpAddrChanged();
    void pcGCodeNameChanged();
    void pcTotalPrintTimeChanged();
    void pcLeftTimeChanged();
    void pcCurPrintLayerChanged();
    void pcPrintSpeedChanged();
    void pcBedDstTempChanged();
    void pcNozzleDstTempChanged();
    void pcBedTempChanged();
    void pcNozzleTempChanged();
    void pcXChanged();
    void pcYChanged();
    void pcZChanged();
    void pcPrinterIDChanged();
    void fanOpenedChanged();
    void ledOpenedChanged();
    void pcGroupNoChanged();

private:
    QString m_name;
    int m_age;
    int m_pcPrinterState;
    bool m_pcHasCamera;
    QString m_pcDeviceName;
    QString m_pcIpAddr;
    QString m_pcGCodeName;
    QString m_pcTotalPrintTime;
    QString m_pcLeftTime;
    int m_pcCurPrintLayer;
    int m_pcPrintSpeed;
    int m_pcBedDstTemp;
    int m_pcNozzleDstTemp;
    int m_pcBedTemp;
    int m_pcNozzleTemp;
    int m_pcX;
    int m_pcY;
    int m_pcZ;
    QString m_pcPrinterID;
    bool m_fanOpened;
    bool m_ledOpened;
    QString m_pcGroupNo;
};
Q_DECLARE_METATYPE(ListModelItem)


class CusListModel : public QAbstractListModel
{
    Q_OBJECT

public:
    explicit CusListModel(QObject *parent = nullptr);
    int rowCount(const QModelIndex &parent = QModelIndex()) const;
    QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
    QHash<int,QByteArray> roleNames() const;

    //开始
    Q_INVOKABLE void start(const QStringList& deviceIDs);
    //暂停
    Q_INVOKABLE void pause(const QStringList& deviceIDs);
    //停止
    Q_INVOKABLE void stop(const QStringList& deviceIDs);
    //添加设备
    Q_INVOKABLE void addDevice(const QString& deviceIP);
    //刷新设备列表
    Q_INVOKABLE void refresh();
    //一键预热
    Q_INVOKABLE void warmUp(const QStringList& deviceIDs);

    //添加分组
    Q_INVOKABLE void addGroup(const QString& groupName = QString());
    //修改组名
    Q_INVOKABLE void editGroupName(const QString& beforeGroupName, const QString& afterGroupName);
    //获取分组
    Q_INVOKABLE QStringList getGroups();

    //signals:

    //public slots:

private:
    QHash<QString, QStringList> m_GroupDevices;
    QHash<QString, ListModelItem*> m_MapItems;
};

#endif // CUSLISTMODEL_H
